package com.itcast.clientx.task;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itcast.clientx.bean.Item;
import com.itcast.clientx.service.ItemService;
import com.itcast.clientx.util.Client;
import com.itcast.clientx.util.TextUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * 任务设置
 */
/*@Component*/
public class ItemTask {

    @Autowired
    private Client client;
    @Autowired
    private TextUtil util;
    @Autowired
    private ItemService service;

    private Scanner scanner = new Scanner(System.in);

    private ObjectMapper mapper = new ObjectMapper();
    /*@Scheduled(fixedDelay = 100 *1000)*/
    public void ItemTask(){
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&wq=%E6%89%8B%E6%9C%BA&page=${page}&s=57&click=0";
        for (int i = 1; i < 10; i = i + 2) {
            String html = client.getHTML(url.replace("${page}",i+""),"");
            this.parseHTML(html);
        }
        System.out.println("手机数据抓取成功");
    }

    /**
     *
     * @param html
     */
    public void parseHTML(String html){

        if(util.notnull(html)){

        }else{
            return;
        }
        Document document =Jsoup.parse(html);
        Elements spus = document.select("div#J_goodsList > ul > li");
        for (Element element : spus) {
            String spu = element.attr("data-spu");
            /*int spu =-1;
            if(attr!=""){
                spu =Integer.parseInt(attr);
            }*/

            Elements skus = element.select("li.ps-item");
            for (Element sku : skus) {
                String next = scanner.next();
                String skuvalue =sku.select("[data-sku]").attr("data-sku");
                Item item = new Item();
                item.setSku(skuvalue);
                List<Item> items = service.find(item);
                /*if(items.size() > 0){
                    continue;
                }*/
                item.setSpu(spu);
                item.setUrl("https://item.jd.com/"+skuvalue+".html");
                /*data-lazy-img*/
                String picture = "https:"+sku.select("img[data-sku]").attr("data-lazy-img");
                picture=picture.replace("n9","n1");
                item.setPicture(picture);
                client.downloadimg(picture);
                String jsonprice = client.getHTML("https://p.3.cn/prices/mgets?skuIds=J_"+skuvalue,"");
                try {
                    String p = mapper.readTree(jsonprice).get(0).get("p").asDouble()+"";
                    item.setPrice(p);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                String detail = client.getHTML(item.getUrl(),"");
                Document document1 = Jsoup.parse(detail);
                String skuname = document1.select("div.sku-name").text();
                item.setTitle(skuname);
                service.save(item);
            }

        }
    }
}
